home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13171 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  109 lines

  1. Path: news.nask.org.pl!usenet
  2. From: piotrpar@blue.maloka.waw.pl (Piotr Parlewicz)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: \\ question fo iostream people //
  5. Date: Sun, 24 Mar 1996 00:06:26 GMT
  6. Organization: Research and Academic Computer Network
  7. Message-ID: <4j23ju$77v@bilbo.nask.org.pl>
  8. References: <4istul$jon@thales.nmia.com>
  9. NNTP-Posting-Host: s114.maloka.waw.pl
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. ghealton@nmia.com (Gilbert Healton) wrote:
  13.  
  14. >How many good ways are there to solve this problem (I am looking for one):
  15.  
  16. >   Set up some type of I/O stream (or stream reference and stream) that
  17. >   allows a program to write to either standard out or a selected file.
  18. >   The ability to switch at will at different points of the program is 
  19. >   vital. Code that works on different compilers is preferred.
  20.  
  21. >In C the code is simple and portable:
  22.  
  23. >    FILE  *Stream = (FILE *)NULL;
  24. >    char  *StreamPath;
  25. >     . . .
  26. >    if ( SomeCondition )
  27. >    {   // time to open a file
  28. >        if ( Stream )        //but, first, if already open
  29. >            fclose( Stream );    ////close the puppy
  30. >        if ( *StreamPath )        
  31. >        Stream = fopen( StreamPath, "w" ); // disk file
  32. >        else
  33. >        Stream = stdout;           // stdout
  34. >    }
  35.  
  36. >If I get some good answers, I'll put them in FAQ format and forward them
  37. >to the C++ FAQ.
  38.  
  39. >-- 
  40. >----- Computer Consulting / Web Pages -----    http://www.nmia.com/~ghealton/
  41. >These opinions are my own. Life is learning and I may retract, modify, 
  42. >even attack, my previous ideas at any time without notice.
  43.  
  44.  
  45. /*
  46. Here is a simple solution that parallels the behaviour of your C code,
  47. minus the fclose of stdout.
  48. In Visual C++ 2.0, cout is an object of the class
  49. 'ostream_withassign',
  50. which allows the cout object to have its output reassigned to another 
  51. stream using the overloaded '=' operator.
  52. But I dont know if that would be portable, so this solution, though
  53. less 
  54. elegant is probably portable.
  55.  
  56. Piotr Parlewicz    piotrpar@blue.maloka.waw.pl  */
  57.  
  58.  
  59. #include <stdio.h>
  60. #include <stdlib.h>          
  61. #include <fstream.h>
  62.  
  63.  
  64. // without arguments, prints to screen
  65. // with argument , prints to file named like the passed argument
  66. int main( int argc, char **argv ) {
  67.  
  68. char *strm_path=NULL;
  69. int     SomeCondition=0 ;
  70. ostream *ofs ;   // common base class for cout and ofstream
  71. int    ofs_file=0; // the primitive solution to knowing if delete is
  72. //needed
  73.  
  74. // for demo only
  75. if( argc == 2 ) 
  76.     strm_path = argv[1]  ;
  77.  
  78. if( argc <= 2 )
  79.     SomeCondition = 1 ;
  80.  
  81. // ... 
  82. if( SomeCondition ) {
  83.     
  84.     if( ofs_file )
  85.         delete ofs ; // this was an ofstream, BTW the C solution tried to
  86.  
  87.         // fclose stdout
  88.  
  89.     // this way, the file is rewritten every time, just like in the 
  90.     // C example, if you want appending, use ios::app instead             
  91.     // of ios::trunc
  92.  
  93.     ofs = strm_path ?
  94.      (ofs_file=1,(ostream *) new ofstream( strm_path, ios::trunc   ) )  :
  95.     (ofs_file=0,(ostream *) &cout ); // cout exists globaly                                                                
  96.     
  97.     // do your output to whatever was set up eariler
  98.     *ofs << "This is a message that will go to file or console\n" ;
  99. }
  100.  
  101.  
  102. // upon program exit insure the file output is flushed
  103. if( ofs_file )    
  104.     delete ofs ;
  105. return(0);
  106. }
  107.  
  108.  
  109.